﻿using System;
using System.Configuration;
using System.ServiceProcess;
using System.Timers;
using System.IO;
using System.Xml;
using Project.Services;
using Project.Services.Tools;
using System.Web;

namespace XlwLineChartNew
{
    public partial class LineChartServiceNew : ServiceBase
    {
        private Timer _timer;

        public LineChartServiceNew()
        {
            InitializeComponent();
        }
        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {

                Exception ex = e.ExceptionObject as Exception;
                ex.ErrorHandle();
            }
            catch { }
        }
        protected override void OnStart(string[] args)
        {
            _timer = new Timer
            {
                Interval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("Timer")) * 1000,
                Enabled = true
            };
            _timer.Elapsed += timer_Elapsed;
        }

        protected override void OnStop()
        {
            _timer.Enabled = false;
        }
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var dateBegin = "";
            var dateEnd = "";
            int times = 0;
            var start = ConfigurationManager.AppSettings.Get("StartTime");
            var end = ConfigurationManager.AppSettings.Get("EndTime");
            var today = DateTime.Today.ToString("yyyy-MM-dd");
            var startTime = DateTime.Parse(today + " " + start);//服务执行开始时间
            var endTime = DateTime.Parse(today + " " + end);//服务执行结束时间
            ("执行时间：" + DateTime.Now).WriteLog();
            if (DateTime.Now >= startTime && DateTime.Now <= endTime)
            {
                ExecuteService.GetData(DateTime.Today.AddDays(-1));
                UserMapService.GetMapUser();

                var config = new XmlDocument();
                var newPath = AppDomain.CurrentDomain.BaseDirectory + "\\";
                config.Load(newPath + "config.xml");
                var root = config.SelectSingleNode("Data");
                if (root != null)
                {
                    var date = root.SelectSingleNode("DateSetting");
                    var begin = date?.SelectSingleNode("BeginDate");
                    var ends = date?.SelectSingleNode("EndDate");
                    var timeTxt = date?.SelectSingleNode("Frequency");
                    if (begin != null) dateBegin = begin.InnerText;
                    if (ends != null) dateEnd = ends.InnerText;
                    if (timeTxt != null) times = int.Parse(timeTxt.InnerText);
                }
                if (!string.IsNullOrEmpty(dateBegin) && !string.IsNullOrEmpty(dateEnd))
                {
                    var begin = DateTime.Parse(dateBegin);
                    var endDate = DateTime.Parse(dateEnd);
                    var days = (endDate - begin).Days;
                    for (int i = 0; i <= days; i++)
                    {
                        if (i + 1 <= times)
                        {
                            var day = begin.AddDays(i);
                            var rows = ExecuteService.GetData(day);
                            if (rows > 0)
                            {
                                var beginNode = root?.SelectSingleNode("DateSetting")?.SelectSingleNode("BeginDate");
                                if (beginNode != null)
                                {
                                    beginNode.InnerText = day.AddDays(1).ToString("yyyy-MM-dd");
                                    if (day == endDate)
                                    {
                                        beginNode.InnerText = "";
                                        var endNode = root.SelectSingleNode("DateSetting")?.SelectSingleNode("EndDate");
                                        if (endNode != null)
                                        {
                                            endNode.InnerText = "";
                                        }
                                    }
                                }
                                config.Save(newPath + "config.xml");
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
